//
//  MQMyProductViewController.m
//  meiquan
//
//  Created by hejingjin on 2017/10/23.
//  Copyright © 2017年 mq. All rights reserved.
//

#import "MQMyProductViewController.h"
#import "MQMyProductCell.h"

#import "MQadviews.h"
#import "MQAdmodels.h"
#import "MQBaseUserInfoModel.h"
#import "MQProductListModel.h"
#import "MQProductViewController.h"
#import "MQAdDetailViewController.h"
@interface MQMyProductViewController ()<UITableViewDataSource,UITableViewDelegate>


@property(nonatomic,strong)UITableView *myproducttableview;

@property(nonatomic,strong)NSMutableArray *dataSource;


@property(nonatomic,strong)MQadviews *adviews;

@property(nonatomic,strong)MQAdmodels *admodels;

@end

@implementation MQMyProductViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.title = @"我的商品";
    _dataSource = [NSMutableArray array];
    
    
    
    
    [self setupMainInterface];
    
    [self getaddata];
}
-(void)getaddata{
    
    NSArray *array = @[@"page1",@"@page2",@"page3"];
    int x = arc4random() % 3;
    
    MQBaseUserInfoModel *userinfo = [MQBaseUserInfoModel shareInstant];
    [userinfo loadUserinfoFromSadeBox];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:userinfo.userId forKey:@"userId"];
    [param setValue:array[x] forKey:@"code"];
    
    [MQRequestTools POSTWithMethod:adv_index params:param success:^(id json) {
        
        NSDictionary *dic = [json valueForKey:@"data"];
        _admodels = [MQAdmodels mj_objectWithKeyValues:dic];
        if (_admodels.advId) {
            [_adviews.adimageviews sd_setImageWithURL:[NSURL URLWithString:_admodels.pic] placeholderImage:[UIImage imageNamed:@""]];
        }else{
            _adviews.bst_height = 0;
            _myproducttableview.bst_y = MAX_Y(_adviews.frame);
            _myproducttableview.bst_height =BSTScreenH -MAX_Y(_adviews.frame);
        }
        
        
        UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [_adviews addGestureRecognizer:tapGesturRecognizer];
        
    } failure:^(NSError *error) {
        
    }];
    
    
}

-(void)tapAction:(UITapGestureRecognizer *)obj{
    NSLog(@"%@",_admodels.url);
    MQAdDetailViewController *advc = [[MQAdDetailViewController alloc] init];
    advc.url = _admodels.url;
     advc.type = _admodels.type;
    [self.navigationController pushViewController:advc animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 97.f;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MQProductListModel *product = _dataSource[indexPath.row];
    MQMyProductCell *cell = [MQMyProductCell cellWIthTableview:tableView];
    [cell.productimageview sd_setImageWithURL:[NSURL URLWithString:product.pic]];
    cell.productnameLabel.text = product.name;
    cell.priceLabel.text =[NSString stringWithFormat:@"¥ %@",product.price];
    cell.storecountLabel.text = [NSString stringWithFormat:@"库存： %ld %@",product.stocks,product.unit];
    cell.producttuanduiLabel.text = [NSString stringWithFormat:@"团队"];
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MQProductListModel *model = _dataSource[indexPath.row];
    MQProductViewController *productvc = [[MQProductViewController alloc] init];
    productvc.model = model;
    [self.navigationController pushViewController:productvc animated:YES];
}

-(void)setupMainInterface{
    
    
    self.view.backgroundColor = BSTColorFromRGB(0xefefef);
    
    _adviews = [MQadviews loadadviews];
    _adviews.bst_x = 0;
    _adviews.bst_width = BSTScreenW;
    _adviews.bst_height = 70 *Persion;
    _adviews.bst_y = NAVHEIGHT;
    [self.view addSubview:_adviews];
    
    [self.view addSubview:self.myproducttableview];
    
    
}

-(UITableView *)myproducttableview{
    if (!_myproducttableview) {
        
        
        _myproducttableview = [[UITableView alloc] initWithFrame:CGRectMake(0, MAX_Y(_adviews.frame), BSTScreenW, BSTScreenH - MAX_Y(_adviews.frame)) style:UITableViewStylePlain];
        _myproducttableview.delegate = self;
        _myproducttableview.dataSource = self;
        _myproducttableview.backgroundColor = BSTColorFromRGB(0xf5f5f5);
        _myproducttableview.showsVerticalScrollIndicator = NO;
        UIView *footerview = [UIView new];
        _myproducttableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        footerview.backgroundColor = BSTColorFromRGB(0xf5f5f5);
        _myproducttableview.tableFooterView = footerview;
        _myproducttableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [self getdata];
            
        }];
        [_myproducttableview.mj_header beginRefreshing];
    }
    return _myproducttableview;
}

-(void)getdata{
    
    
    MQBaseUserInfoModel *userinfo = [MQBaseUserInfoModel shareInstant];
    [userinfo loadUserinfoFromSadeBox];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:userinfo.userId forKey:@"userId"];
    
    
    [MQRequestTools POSTWithMethod:product_agentProduct params:param success:^(id json) {
        NSArray *customlist = [json valueForKey:@"data"];
        
        [_dataSource removeAllObjects];
        for (NSDictionary *dic in customlist) {
            MQProductListModel *product = [MQProductListModel mj_objectWithKeyValues:dic];
            [_dataSource addObject:product];
            
        }
        
        [_myproducttableview reloadData];
        [_myproducttableview.mj_header endRefreshing];
    } failure:^(NSError *error) {
        
    }];
    
    
    
}
@end
